package application.common.cron4j;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.cron4j.ITask;

import application.common.model.StoreMap;
import application.common.render.HttpClientResult;
import application.common.util.HttpUtil;
import application.common.util.ShiroUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class StoreMapTask implements ITask{
	
	private boolean isEnd=true;
	
	public void run() {
		if(isEnd) {
			if(ShiroUtil.getConfig()!=null && !StrKit.isBlank(ShiroUtil.getConfig().getSynURL())) {
				isEnd=false;
				try {
					HttpClientResult httpClientResult=HttpUtil.doGet(String.format("%s/api/cab/store/getMaps?cabinetId=%s", ShiroUtil.getConfig().getSynURL(),ShiroUtil.getAuth()));
					if(httpClientResult==null) {
						isEnd=true;
						return;
					}
					if(httpClientResult.getCode()!=200) {
						isEnd=true;
						return;	
					}
					String content=httpClientResult.getContent();
					if(StrKit.isBlank(content)) {
						isEnd=true;
						return;	
					}
					JSONObject jsonObject=JSONObject.fromObject(content);
					if(jsonObject==null) {
						isEnd=true;
						return;	
					}
					if(jsonObject.has("code")) {
						int code=jsonObject.getInt("code");
						if(code!=200) {
							isEnd=true;
							return;	
						}
						if(jsonObject.has("data")) {
							String data=jsonObject.getString("data");
							if(StrKit.isBlank(data)) {
								isEnd=true;
								return;	
							}
							JSONArray array=JSONArray.fromObject(data);
							if(array==null || array.size()==0) {
								isEnd=true;
								return;
							}
							StringBuffer sb=new StringBuffer();
							for(Object object:array) {
								JSONObject row=JSONObject.fromObject(object);
								if(row!=null) {
									String id=row.getString("id");
									StoreMap storeMap=StoreMap.dao.findById(id);
									if(storeMap==null) {
										storeMap=new StoreMap();
									}
									
									if(!StrKit.equals(storeMap.getColumnLabel(), row.getString("label"))) {
										storeMap.setColumnLabel(row.getString("label"));
									}
									if(!StrKit.equals(storeMap.getColumnValue(), row.getString("value"))) {
										storeMap.setColumnValue(row.getString("value"));
									}
									
									if(!StrKit.equals(storeMap.getSort(), row.getString("serial"))) {
										storeMap.setSort(row.getString("serial"));
									}
									
									if(!StrKit.equals(storeMap.getClick(), row.getString("isClick"))) {
										storeMap.setClick(row.getString("isClick"));
									}
									if(StrKit.isBlank(storeMap.getId())) {
										storeMap.setId(id);
										storeMap.save();
									}
									else {
										storeMap.update();
									}
									sb.append("'").append(storeMap.getId()).append("'").append(",");
								}
							}
							if(!StrKit.isBlank(sb.toString())) {
								Db.delete("delete from "+StoreMap.TABLE_NAME+" where id not in ("+(sb.substring(0,sb.length()-1).toString())+") "); 
							}
							isEnd=true;
						}else {
							isEnd=true;
						}
					}else {
						isEnd=true;
					}
				}catch (Exception e) {
					isEnd=true;
				}
			}
			isEnd=true;
		}
	}

	public void stop() {
	}

}
